﻿using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Gmantis.Controls.DataGrid
{
    public enum DataGridUnitType
    {
        Auto,
        Pixel,
        Star,
        AutoStar
    }

    [StructLayout(LayoutKind.Sequential), TypeConverter(typeof(DataGridLengthTypeConverter))]
    public struct DataGridLength
    {
        private double _value;
        private DataGridUnitType _type;
        public DataGridLength(double pixels)
        {
            this._type = DataGridUnitType.Pixel;
            this._value = pixels;
        }

        public DataGridLength(double value, DataGridUnitType type)
        {
            this._type = type;
            this._value = value;
        }

        public DataGridUnitType Type
        {
            get
            {
                return this._type;
            }
        }
        public double Value
        {
            get
            {
                return this._value;
            }
        }
        public bool IsAbsolute
        {
            get
            {
                return (this._type == DataGridUnitType.Pixel);
            }
        }
        public bool IsAuto
        {
            get
            {
                return (this._type == DataGridUnitType.Auto);
            }
        }
        public bool IsStar
        {
            get
            {
                return (this._type == DataGridUnitType.Star);
            }
        }
        public bool IsAutoStar
        {
            get
            {
                return (this._type == DataGridUnitType.AutoStar);
            }
        }
        public static DataGridLength Auto
        {
            get
            {
                return new DataGridLength(1.0, DataGridUnitType.Auto);
            }
        }
        public static bool operator ==(DataGridLength gl1, DataGridLength gl2)
        {
            return ((gl1._type == gl2._type) && (gl1._value == gl2._value));
        }

        public static bool operator !=(DataGridLength gl1, DataGridLength gl2)
        {
            return !(gl1 == gl2);
        }

        public override bool Equals(object obj)
        {
            return ((obj is DataGridLength) && (this == ((DataGridLength)obj)));
        }

        public override int GetHashCode()
        {
            return (this._type.GetHashCode() ^ this._value.GetHashCode());
        }

        public override string ToString()
        {
            switch (this._type)
            {
                case DataGridUnitType.Pixel:
                    return string.Format("{0}px", this._value);

                case DataGridUnitType.Star:
                    if (this._value == 1.0)
                    {
                        return "*";
                    }
                    return string.Format("{0}*", this._value);

                case DataGridUnitType.AutoStar:
                    if (this._value == 1.0)
                    {
                        return "Auto*";
                    }
                    return string.Format("{0}Auto*", this._value);
            }
            return "Auto";
        }
    }
}
